package com.arvinclub.web.controller.fun;

import com.arvinclub.service.service.BlogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 查找内容
 */
@Controller
public class SearchController {
    @Resource
    private BlogService blogService;

    /**
     * 进入查找内容页面
     */
    @GetMapping("user/search.html")
    public String search() {
        return "search";
    }

    /**
     * 按关键词查找内容（默认第一页）
     */
    @PostMapping("user/search.html")
    public String search(@RequestParam String keyword, HttpSession session) {
        session.setAttribute("keyword", keyword);
        return searchByPage(1, session);
    }

    /**
     * 按关键词查找内容（指定页号）
     */
    @GetMapping("user/search.html/{page}")
    public String searchByPage(@PathVariable int page, HttpSession session) {
        /*确认关键词*/
        String keyword = (String) session.getAttribute("keyword");
        if (StringUtils.isBlank(keyword))
            return "redirect:/user/search.html";
        /*开始查询,返回信息*/
        session.setAttribute("blogPage", blogService.selectBlogsByKey(keyword, page));
        return "search";
    }

}
